package com.atguigu.gulimall.member.service.impl;

import com.atguigu.gulimall.member.dao.MemberLevelDao;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.exception.PhoneExsitException;
import com.atguigu.gulimall.member.exception.UserNameExsitException;
import com.atguigu.gulimall.member.service.MemberLevelService;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberRegistVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;

import javax.annotation.Resource;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Resource
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        MemberEntity entity = new MemberEntity();

        //设置默认等级
        LambdaQueryWrapper<MemberLevelEntity> levelWrapper = new LambdaQueryWrapper<>();
        levelWrapper.eq(MemberLevelEntity::getDefaultStatus,1);
        MemberLevelEntity memberLevelEntity = memberLevelDao.selectOne(levelWrapper);
        entity.setLevelId(memberLevelEntity.getId());

        //检查用户名 和手机号是否唯一。  为了让controller 能感知异常  使用异常机制
        checkPhoneUnique(vo.getPhone());
        checkUserNameUnique(vo.getUserName());

        entity.setUsername(vo.getUserName());
        entity.setMobile(vo.getPhone());

        //设置密码 密码加密存储
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(vo.getPassword());
        entity.setPassword(encode);

        baseMapper.insert(entity);
    }

    @Override
    public void checkEmailUnique(String email) {
    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameExsitException{
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getUsername,userName);
        Integer integer = baseMapper.selectCount(wrapper);
        if(integer>0){
            throw  new UserNameExsitException();
        }
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExsitException{
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getMobile,phone);
        Integer integer = baseMapper.selectCount(wrapper);
        if(integer>0){
            throw  new PhoneExsitException();
        }

    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String account = vo.getLoginacct();
        String password = vo.getPassword();
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getUsername,account).or().eq(MemberEntity::getMobile,account);
        MemberEntity entity = baseMapper.selectOne(wrapper);
        if(entity==null){
            return null;
        }else {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(password, entity.getPassword());
            return matches?entity:null;
        }
    }

}